package com.hst.capacity.facade.rest;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.DingTalkBiz;
import com.hst.capacity.application.biz.OrderPoolBiz;
import com.hst.capacity.application.biz.TokenBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.auth.TokenEntity;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.express.ExListNewParam;
import com.hst.capacity.domain.model.request.intercity.IcListParam;
import com.hst.capacity.domain.model.request.orderpool.OrderPoolAiParam;
import com.hst.capacity.domain.model.request.orderpool.OrderPoolParam;
import com.hst.capacity.domain.model.request.price.OrderPoolPriceParam;
import com.hst.capacity.domain.model.request.specialcar.SpListParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.intercity.IcSearchVO;
import com.hst.capacity.domain.service.TokenService;
import com.hst.capacity.infra.convertor.DingTalkConvertor;
import com.hst.capacity.infra.external.dingtalk.model.request.order.NewOrderRemindParam;
import com.hst.capacity.infra.persistence.jpa.order.pool.OrderPoolRepository;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  订单池模块Controller
 *
 * */
@Slf4j
@RequestMapping("/order/pool")
@RestController
public class OrderPoolController {

    @Autowired
    private OrderPoolBiz orderBiz;

    @Autowired
    OrderPoolRepository orderPoolRepository;

    @Autowired
    DingTalkBiz dingTalkBiz;

    @Autowired
    TokenBiz tokenBiz;

    /**
     * 计算订单价格/里程/耗时
     * @param param
     * @return
     */
    @PostMapping("/price")
    private DefaultVO getOrderPrice(@RequestBody @Valid OrderPoolPriceParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.newOrderPoolPrice(param, false));
    }

    /**
     * 计算订单价格/里程/耗时
     * @param param
     * @return
     */
    @PostMapping("/common/price")
    private DefaultVO getOrderCommonPrice(@RequestBody @Valid OrderPoolPriceParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.orderPoolCommonPrice(param, false));
    }

    /**
     * 计算订单价格/里程/耗时
     * @param param
     * @return
     */
    @PostMapping("/ai/price")
    private DefaultVO getAiOrderPrice(@RequestBody @Valid OrderPoolPriceParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.orderPoolCommonPrice(param, false));
    }

    /**
     * 订单保存
     * @param param
     * @return
     */
    @PostMapping("/save")
    private DefaultVO savePoolOrder(@RequestBody @Valid OrderPoolParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.savePoolOrder(param).getIdOrder());
    }

    /**
     * 订单保存
     * @param param
     * @return
     */
    @PostMapping("/common/save")
    private DefaultVO saveCommonPoolOrder(@RequestBody @Valid OrderPoolParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.createPoolCommonOrder(param).getIdOrder());
    }

    /**
     * 创建订单
     * @param param
     * @return
     */
    @PostMapping("/ai/create")
    private DefaultVO createAiPoolOrder(@RequestBody @Valid OrderPoolAiParam param){
        return success(orderBiz.createAiPoolOrder(param).getIdOrder());
    }

    /**
     * 创建订单
     * @param param
     * @return
     */
    @PostMapping("/create")
    private DefaultVO createPoolOrder(@RequestBody @Valid OrderPoolParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.createPoolCommonOrder(param).getIdOrder());
    }

    /**
     * 更新订单
     * @param param
     * @return
     */
    @PostMapping("/update")
    private DefaultVO updatePoolOrder(@RequestBody @Valid OrderPoolParam param){
        if (Constants.OrderBizType.Airport==param.getOrderBizType() && StrUtil.isEmpty(param.getIdAirport()))
            throw new BusinessException(4001,"机场不能为空");
        if (Constants.OrderBizType.RailStation==param.getOrderBizType() && StrUtil.isEmpty(param.getIdRailwayStation()))
            throw new BusinessException(4001,"火车站不能为空");
        return success(orderBiz.updatePoolOrder(param).getIdOrder());
    }

    /**
     * 取消订单
     * @param param
     * @return
     */
    @PostMapping("/cancel")
    private DefaultVO cancelPoolOrder(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        orderBiz.cancelPoolOrder(idOrder);
        return success(MapUtil.builder("idOrder","订单已删除成功").build());
    }

    /**
     * 获取今日录单信息
     * @param param
     * @return
     */
    @PostMapping("/list/new")
    private DefaultVO getNewExOrderList(@RequestBody @Valid SpListParam param, HttpServletRequest request){
        param.setHistoryFlg(false);
        param.setIdDispatcher(tokenBiz.findIdUser(request));
        return success(orderBiz.getTodayOrderList(param));
    }

    /**
     * 订单检索查询
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO search(@RequestBody @Valid SpListParam param, HttpServletRequest request){
        param.setIdDispatcher(tokenBiz.findIdUser(request));
        return success(orderBiz.search(param));
    }

    /**
     * 获取指定城市今日录单信息
     * @param param
     * @return
     */
    @PostMapping("/list/region/today")
    private DefaultVO getNewOrderListByRegionCode(@RequestBody @Valid SpListParam param){
        return success(orderBiz.getNewOrderListByRegionCode(param));
    }

    /**
     * 订单详情
     * @param param
     * @return
     */
    @PostMapping("/detail")
    private DefaultVO orderDetail(@RequestBody @Valid Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.spOrderDetail(idOrder));
    }

    /**
     * 订单支付信息
     * @param param
     * @return
     */
    @PostMapping("/billInfo")
    private DefaultVO orderBillInfo(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.getOrderBillInfo(idOrder));
    }

    /**
     * 订单修改
     * @param param
     * @return
     */
    @PostMapping("/info")
    private DefaultVO orderInfo(@RequestBody @Valid Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(orderBiz.orderPoolInfo(idOrder));
    }

    /**
     * 更换订单派遣司机
     * @param param
     * @return
     */
    @PostMapping("/redispatch/driver")
    private DefaultVO reDispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        if (orderBiz.reDispatchDriver(param))
            return success(MapUtil.builder("success","订单:"+param.getIdOrder()+"指派成功").build());
        else
            return success(MapUtil.builder("success","订单:"+param.getIdOrder()+"指派失败").build());
    }

    /**
     * 虚拟接单
     * @param param
     * @return
     */
    @PostMapping("/virtual/accept")
    private DefaultVO virtualDispatchDriver(@RequestBody @Valid DispatchParam param){
        // todo 虚拟接单
        return success(orderBiz.virtualAcceptOrder(param));
    }

//    /**
//     * 虚拟接单
//     * @param param
//     * @return
//     */
//    @PostMapping("/send/dingtalk")
//    private DefaultVO sendDingTalk(@RequestBody @Valid DispatchParam param){
//        try {
//            OrderPoolEntity entity = orderPoolRepository.findByIdOrder(param.getIdOrder());
//            NewOrderRemindParam poolParam = DingTalkConvertor.INSTANCE.convert(entity);
//            dingTalkBiz.sendNewOrderRemind(poolParam);
//        } catch (Exception e) {
//            log.info("sendDingTalk:" + e.getMessage());
//        }
//        // todo 虚拟接单
//        return success(MapUtil.builder("success","消息发送成功").build());
//    }

}
